package com.sy.department.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sy.department.dao.po.DepartmentPo;
import com.sy.department.web.IDepartmentController;
import com.sy.department.web.fo.ArgsQueryDepartment;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 部门信息表 服务类
 * </p>
 *
 * @author Administrator
 * @since 2025-04-08
 */
public interface IDepartmentService extends IService<DepartmentPo> {

    default List<DepartmentPo> listDepartment(IDepartmentController.ReqQueryDepartment fo) {
        LambdaQueryWrapper<DepartmentPo> wrapper = buildQueryWrapper(fo);
        return this.getBaseMapper().selectList(wrapper);
    }

    default Page<DepartmentPo> pageDepartment(IDepartmentController.ReqQueryDepartment fo) {
        LambdaQueryWrapper<DepartmentPo> wrapper = buildQueryWrapper(fo);
        Page<DepartmentPo> page = new Page<>(fo.getCurrentPage(), fo.getPageSize());
        return this.getBaseMapper().selectPage(page, wrapper);
    }

    default LambdaQueryWrapper<DepartmentPo> buildQueryWrapper(IDepartmentController.ReqQueryDepartment fo) {
        LambdaQueryWrapper<DepartmentPo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!Objects.isNull(fo.getId()), DepartmentPo::getId, fo.getId())
                .eq(StringUtils.hasLength(fo.getName()), DepartmentPo::getName, fo.getName())
                .eq(StringUtils.hasLength(fo.getLocation()), DepartmentPo::getLocation, fo.getLocation());

        return wrapper;
    }
}
